﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GetMyNewDoc]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_GetMyNewDoc];
GO
CREATE PROCEDURE [dbo].[sproc_GetMyNewDoc]
    @UserName nvarchar(300),
    @RightCode int = 10,
    @Inherit bit =0
/*

============================================================
功能:    得到用户名的所有地方的最新文档
参数:
    @UserName nvarchar(300)        :    用户名
    @RightCode int =10        :    权利代号（默认10）
备注:可能优化
============================================================

*/
AS
BEGIN

SET NOCOUNT ON

--得到用户名有审批权的CLASSID表
declare @tmp table (ClassID int);

INSERT INTO @tmp 
EXEC dbo.sproc_GetAllClassToAccess @UserName,@RightCode,@Inherit;


--得到间接的浏览权，即职位对其下属的项目监督权
IF EXISTS(SELECT 1 FROM @tmp WHERE ClassID = 0) 
BEGIN
    SELECT a.DocID,a.ClassID,DocTitle,a.DocApprover,a.DocApproveDate,a.DocViewedTimes,a.DocType,a.DocAddedBy,convert(nvarchar(10),a.DocAddedDate,120) as DocAddedDate, b.classname 
        FROM 
            uds_document a,
            uds_class b 
        WHERE     a.classid = b.classid 
            and a.docdeletion =0 
            and a.doctype =0 
            and a.docapproved=1 
        ORDER BY a.DocApproveDate DESC

    PRINT '全局'
    RETURN 0 
END

--得到所有的文档
Select a.DocID,a.ClassID,DocTitle,a.DocApprover,a.DocApproveDate,a.DocViewedTimes,a.DocType,a.DocAddedBy,convert(nvarchar(10),a.DocAddedDate,120) as DocAddedDate, b.classname   
    FROM 
        uds_document a,
        uds_class b 
    WHERE     a.classid = b.classid and a.ClassID IN (SELECT ClassID FROM @tmp) 
        and a.docdeletion =0 
        and a.doctype =0 
        and a.docapproved=1 
    ORDER BY a.DocApproveDate DESC

END
GO